//
//  SEMenuAnimateView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/25.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
protocol SEMenuAdverDelegate:class {
    
    /// 打开广告选择页面
    ///
    /// - Parameter buyAdsBool: 购买 or 出售
    /// - Returns: void
    func openAdverment(buyAdsBool:Bool) -> Void
}
class SEMenuAnimateView: UIView {
    let BtnTag = 1000
    var selectBtn:UIButton? = nil
    weak var delegate:SEMenuAdverDelegate?
    func show() -> Void {
        self.frame = CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: kSCREEN_HEIGHT)
        self.themeBackground = ThemeManager.ThemeColorKeys.background
        UIApplication.shared.keyWindow?.addSubview(self)
        
        selectBtn = UIButton(frame: CGRect(x: (kSCREEN_WIDTH-52)/2, y: kSCREEN_HEIGHT - 72, width: 52, height: 52))
        selectBtn?.addTarget(self, action: #selector(selectAction), for: UIControlEvents.touchUpInside)
        selectBtn?.setBackgroundImage(UIImage(named: "closeAdver"), for: UIControlState.normal)
        self.addSubview(selectBtn!)
        
        for i in 0..<2 {
            let distnce = i==1 ? kSCREEN_WIDTH/2+35 : kSCREEN_WIDTH/2-115
            let btn = SEMenuButtton(frame: CGRect(x:distnce, y: kSCREEN_HEIGHT-220, width: 80, height: 104))
            btn.addTarget(self, action: #selector(openAdvert), for: UIControlEvents.touchUpInside)
            btn.nameLabel?.text = i==0 ? kText("发布购买广告") : kText("发布出售广告")
            btn.photoImageView?.image = UIImage(named: i == 0 ? "buyAdvert" : "sendAdvert")
            btn.photoImageView?.snp.makeConstraints({ (make) in
                make.top.equalToSuperview().offset(20)
                make.centerX.equalToSuperview()
            })
            btn.nameLabel?.font = UIFont.systemFont(ofSize: 12)
            btn.nameLabel?.snp.makeConstraints({ (make) in
                make.bottom.equalToSuperview()
                make.centerX.equalToSuperview()
            })
            btn.tag = BtnTag+i
            self.addSubview(btn)
            self.annimation(btn: btn, open: true)
        }
    }
    
    
    @objc func openAdvert(btn:SEMenuButtton) -> Void {
        self.selectAction()
        let selectBool = btn.tag == 1000 ? true : false
        if delegate != nil {
            self.delegate?.openAdverment(buyAdsBool: selectBool)
        }
    }

    @objc func selectAction() -> Void {
        for i in 0..<2 {
            let btn = self.viewWithTag(i+BtnTag)
            self.annimation(btn:btn as! SEMenuButtton , open: false)
        }
        
    }
    
    
    
    func annimation(btn: SEMenuButtton, open:Bool) -> Void {
        var  animation:CABasicAnimation;
        animation = CABasicAnimation(keyPath: "position")
        animation.duration = 0.3
        animation.isRemovedOnCompletion = false
        animation.fillMode = kCAFillModeForwards
        
        
        let scaleAnimation = CABasicAnimation(keyPath: "transform")
        scaleAnimation.duration = 0.3;
        scaleAnimation.isCumulative = false;
        animation.fillMode = kCAFillModeForwards
        animation.isRemovedOnCompletion = false
        scaleAnimation.timingFunction =  CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        
        
        if open {
            animation.fromValue = NSValue(cgPoint: (selectBtn?.layer.position)!)
            animation.toValue = NSValue(cgPoint: btn.layer.position )
            scaleAnimation.fromValue = NSValue(caTransform3D:CATransform3DMakeScale(0.5, 0.5, 1))
            scaleAnimation.toValue = NSValue(caTransform3D:CATransform3DMakeScale(1, 1, 1))
        }else {
            animation.toValue = NSValue(cgPoint: (selectBtn?.layer.position)!)
            animation.fromValue = NSValue(cgPoint: btn.layer.position )
            scaleAnimation.toValue = NSValue(caTransform3D:CATransform3DMakeScale(0, 0, 0))
            scaleAnimation.fromValue = NSValue(caTransform3D:CATransform3DMakeScale(1, 1, 1))
            
        }
        
        let group = CAAnimationGroup()
        group.duration = 0.3
        group.isRemovedOnCompletion = false
        group.fillMode = kCAFillModeForwards
        group.animations = [animation,scaleAnimation]
        btn.layer.add(group, forKey: "animationOpacity")
        
        if !open {
            self.removeFromSuperview()
        }
    }
    
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
